<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--选择入库记录-->
<div class="modal fade" id="modal-select">
    <div class="modal-dialog" style="width:1200px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择入库记录</h4>
            </div>
            <div class="modal-body">
                <table id="src-table"></table>
                <form id="storeForm" onsubmit="return false;">
                    <div class="row" style="margin-top: 20px">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px;">
                                <label>项目编码</label>
                                <input type="text" name="odaProCode" class="form-control" placeholder="" style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>项目名称</label>
                                <input type="text" name="odaProName" class="form-control" placeholder="" style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>物料描述</label>
                                <input type="text" name="odaMatDesc" class="form-control" placeholder="" style="width: 100px">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>计划跟踪号</label>
                                <input type="text" name="odaTrackCode" class="form-control" placeholder="" style="width: 100px">
                            </div>
                            <button type="button" class="btn btn-success" id="btn-search-store">
                                <span class="Bold">搜索</span>
                            </button>
                        </div>
                    </div>
                </form>
                <table id="store-table"></table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<!--查看-->
<div class="modal fade" id="modal-info">
    <div class="modal-dialog" style="width:800px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">详细信息</h4>
            </div>
            <div class="modal-body">
                <div id="info">
                    <div class="col-md-12">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">类型</label>
                                <div class="col-sm-9">
                                    <span id="typeName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">材质</label>
                                <div class="col-sm-9">
                                    <span id="qualityName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">规格</label>
                                <div class="col-sm-9">
                                    <span id="specName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">宽度</label>
                                <div class="col-sm-9">
                                    <span id="matWidth"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">长度</label>
                                <div class="col-sm-9">
                                    <span id="matLength"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">米重/厚度</label>
                                <div class="col-sm-9">
                                    <span id="matHeight"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">数量</label>
                                <div class="col-sm-9">
                                    <span id="odaNum"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">重量</label>
                                <div class="col-sm-9">
                                    <span id="odaWeight"></span>
                                </div>
                            </div>
                        </div>
                        <div shiro:hasPermission="plan:amount:view" class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">单价</label>
                                <div class="col-sm-9">
                                    <span id="odaPrice"></span>
                                </div>
                            </div>
                        </div>
                        <div shiro:hasPermission="plan:amount:view" class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">金额</label>
                                <div class="col-sm-9">
                                    <span id="odaAmount"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">跟踪号</label>
                                <div class="col-sm-9">
                                    <span id="odaTrackCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">项目编码</label>
                                <div class="col-sm-9">
                                    <span id="odaProCode"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">项目名称</label>
                                <div class="col-sm-9">
                                    <span id="odaProName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">在库类型</label>
                                <div class="col-sm-9">
                                    <span id="whiName"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">回货时间</label>
                                <div class="col-sm-9">
                                    <span id="odaBackDate"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">回货重量</label>
                                <div class="col-sm-9">
                                    <span id="odaBackWeight"></span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-3 text-right">特殊说明</label>
                                <div class="col-sm-9">
                                    <span id="odaSpec"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header"><h1>原材料回货确认</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="rppMatDesc" class="form-control" placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="rppProjectCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="rppProjectName" class="form-control" placeholder="请输入项目名称">
                                </div>

                                <!--<div class="form-group">
                                    <label>跟踪号</label>
                                    <input type="text" name="sppTrackCode" class="form-control" placeholder="">
                                </div>-->
                                <div class="form-group">
                                    <label>确认状态</label>
                                    <select name="rppRetStatus" class="form-control">
                                        <option value="">全部</option>
                                        <option value="0" selected="selected">未确认</option>
                                        <option value="1">已确认</option>
                                    </select>
                                </div>
                                <!--<div class="form-group">
                                    <label>预入库状态</label>
                                    <select name="sppPreStatus" class="form-control">
                                        <option value="" selected="selected">全部</option>
                                        <option value="0">未到货</option>
                                        <option value="1">预入库</option>
                                    </select>
                                </div>-->
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                   <!--     <div class="row" style="margin-top: 10px;padding-bottom: 5px">
                            <div class="form-inline">
                                <div class="form-group col-sm-3" style="width: 290px;padding: 0px;margin: 0px; margin-right: 10px">
                                    <label style="width: 60px;text-align: right">提报日期</label>
                                    <input type="text" class="form-control datepicker" style="width: 102px;" name="startTime" readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" style="width: 102px" name="endTime" readonly="readonly"/>
                                </div>

                                <a href="javascript:void(0);" class="btn btn-success pull-right" style="margin-right: 10px" onclick="submitAll()">
                                    <span class="Bold">全部确认</span>
                                </a>

                            </div>
                        </div>-->
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table, srcTable, storeTable, planId;

    /** 打开选择模态框 */
    function openSelectModal(row) {
        let data = [];
        data.push(row);
        planId = row.rppId;
        js.table.load(srcTable, data);
        js.table.search(storeTable);
        js.modal.open("modal-select");
    }

    /** 查看 */
    function openInfoModal(row) {
        $("#info").fillSpan(row);
        js.modal.open("modal-info");
    }

    /** 确认回货 */
    function submitReturn(rppId, odaId) {
        js.confirmPost({
            content: "确认回货吗？",
            url: ctx + "buy/materialReturn/submit",
            data: {
                rppId: rppId,
                odaId: odaId
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    js.modal.hide("modal-select");
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    /** 批量确认回货 */
    function submitAll() {
        var arr = js.table.selectColumns(table, "rppId");
        if (arr.length < 1) {
            js.modal.warning("请选择要确认回货的采购计划");
            return;
        }
        js.post({
            url: ctx + "buy/materialReturn/submit/all",
            data: {
                rppId: js.table.selectColumns(table, "sppId"),
                odaId: js.table.selectColumns(table, "odaId")
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    /** 撤消回货确认 */
    function cancel(id) {
        js.confirmPost({
            content: "删除数据将无法恢复，确认删除吗？",
            url: ctx + "buy/materialReturn/cancel",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    $(function () {
        $("title").html($("title").html()+" —— 原材料回货确认");
        /** 初始化表格 */
        table = js.table.init({
            url: ctx + "buy/materialReturn/list",
            showColumns: false,
            showExport: false,
            pageSize: 300,
            columns: [
                {
                    checkbox: true,
                    width:'30px',
                    formatter: function (value, row) {
                        if (sessionUser.name === row.rppPlanner && row.odaId && !row.sppOdaId) {
                            return {disabled: false}
                        } else {
                            return {disabled: true}
                        }
                    }
                },
                {
                    title: '序号', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                { title: "计划员", field: 'rppPlanner'},
                { title: "提报计划<br>物料描述", field: 'rppMatDesc'},
                { title: "提报计划<br>数量", field: 'rppNum'},
                { title: "提报计划<br>单位", field: 'rppUnit'},
                { title: "提报计划<br>项目编号", field: 'rppProjectCode'},
                { title: "提报计划<br>项目名称", field: 'rppProjectName'},
                { title: "采购计划<br>物料编码", field: 'sppSapCode'},
                { title: "采购计划<br>跟踪号", field: 'sppTrackCode'},
                // { title: "提报计划<br>跟踪号", field: 'rppTrackCode'},
                { title: "提报计划<br>确认状态", field: 'rppRetStatus',
                    formatter: function (value, row, index, field) {
                        if (value) {
                            return '已确认';
                        }
                        return '未确认';
                    }},
                //（类型、材质、规格、宽度、长度、米重/厚度）界面摆不下就拼成一个字段、数量、重量、计划跟踪号、项目编码、项目名称、回货时间
                { title: "入库记录<br>物料描述", field: 'matNameDesc'},
                // { title: "米重/厚度", field: 'matHeight'},
                // { title: "数量", field: 'odaNum'},
                { title: "入库记录<br>重量", field: 'odaWeight'},
                { title: "入库记录<br>计划跟踪号", field: 'odaTrackCode'},
                { title: "入库记录<br>项目编码", field: 'odaProCode'},
                { title: "入库记录<br>项目名称", field: 'odaProName'},
                { title: "入库记录<br>物料编码", field: 'odaMatSapCode'},
                {
                    title: "操作",
                    width: '60px',
                    formatter: function (value, row, index) {
                        let actions = [];
                        if (row.odaId != null) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="openInfoModal(' + js.jsonStringify(row) + ')"> 查看</a> ');
                        }
                        if(sessionUser.name === row.rppPlanner){
                            if (row.rppOdaId === null && row.odaId != null && hasP("plan:materialretconfirm:confirm") && row.rppRetStatus != 1) {
                                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="submitReturn(' + row.rppId + ','+row.odaId+')"> 确认</a> ');
                            }
                            if (row.rppOdaId != null && hasP("plan:materialretconfirm:confirm") && row.rppRetStatus == 1) {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="cancel(' + row.rppId + ')"> 撤消</a> ');
                            }
                            if (hasP("plan:materialretconfirm:confirm") && row.rppRetStatus != 1) {
                                actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openSelectModal(' + js.jsonStringify(row) + ')"> 选择</a> ');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });
        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /** 原数据表格 */
        srcTable = js.table.init({
            id: "src-table",
            showExport: false,
            url: false,
            showColumns: false,
            pagination: false,
            sidePagination: null,
            firstLoad: false,
            columns: [
                { title: "提报计划<br>物料描述", field: 'rppMatDesc'},
                // { title: "SAP<br>物料编码", field: 'rppSapCode'},
                { title: "提报计划<br>数量", field: 'rppNum'},
                { title: "提报计划<br>单位", field: 'rppUnit'},
                { title: "提报计划<br>项目编号", field: 'rppProjectCode'},
                { title: "提报计划<br>项目名称", field: 'rppProjectName'},
                // { title: "提报计划<br>跟踪号", field: 'rppTrackCode'},
                { title: "提报计划<br>回货状态", field: 'rppRetStatus',
                    formatter: function (value, row, index, field) {
                        if (value) {
                            return '已回货';
                        }
                        return '未回货';
                    }},
            ]
        });
        /** 入库记录表格 */
        storeTable = js.table.init({
            id: "store-table",
            queryForm: "storeForm",
            url: ctx + "buy/materialReturn/store/list",
            showColumns: false,
            showExport: false,
            firstLoad: false,
            columns: [
                {
                    title: '序号', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                { title: "物料描述", field: 'matNameDesc'},
                { title: "米重/厚度", field: 'matHeight'},
                { title: "数量", field: 'odaNum'},
                { title: "重量", field: 'odaWeight'},
                { title: "计划跟踪号", field: 'odaTrackCode'},
                { title: "项目编码", field: 'odaProCode'},
                { title: "项目名称", field: 'odaProName'},
                { title: "回货时间", field: 'odaBackDate'},
                {
                    title: "操作",
                    width: '60px',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if(row.rppRetStatus != 1){
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="submitReturn(' + planId + ',' + row.odaId + ')"> 确认</a> ');
                        }
                        return actions.join('');
                    }
                }
            ]
        });
        /** 入库记录表格搜索 */
        $("#btn-search-store").click(function () {
            js.table.search(storeTable);
        });
        /** 选择入库记录模态框隐藏事件，重置查询表单*/
        js.modal.hideEvent("modal-select", function () {
            js.reset("storeForm");
        });

    });

</script>
</body>
</html>
